package com.tfleo.mealssenger;

import java.util.ArrayList;
import com.tfleo.mealssenger.R;
import com.tfleo.mealssenger.adapter.HangoutListAdapter;
import com.tfleo.mealssenger.adapter.UserListAdapter;
import com.tfleo.mealssenger.adapter.WantedListAdapter;
import com.tfleo.mealssenger.object.Food;
import com.tfleo.mealssenger.object.Hangout;
import com.tfleo.mealssenger.object.User;
import com.tfleo.mealssenger.utility.DataFetcher;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class HangoutBrowseActivity extends Activity {
	private LayoutInflater mInflater;
	private SharedPreferences mSharedPref;
	
	private ListView vHangoutList;
	
	private ArrayList<Hangout> mHangout;
	private HangoutListAdapter mHangoutAdapter;
	
	private Context mContext;
	private String mAuth;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.hangout_browse_layout);
		
		mContext = this;
		mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		mSharedPref = getSharedPreferences(Main.USER_DATA, MODE_PRIVATE);
		mAuth = Secure.getString(this.getContentResolver(), Secure.ANDROID_ID); //for authentication
		
		vHangoutList = (ListView) findViewById(R.id.hangout_list);
		
		FetchHangoutTask task = new FetchHangoutTask();
		task.execute(mSharedPref.getInt(Main.USER_ID, 0));
	}
	
	private class FetchHangoutTask extends AsyncTask<Integer, Void, Boolean> {
		private ProgressDialog mProgressDialog;
		@Override    
	    protected void onPreExecute() 
	    {       
	        super.onPreExecute();
	        mProgressDialog = new ProgressDialog(mContext);
	        mProgressDialog.setMessage("Loading...");
	        mProgressDialog.setCancelable(false);
	        mProgressDialog.setIndeterminate(true);
	        mProgressDialog.show();
	    }
		
		@Override
		protected Boolean doInBackground(Integer... id) {
			mHangout = DataFetcher.fetchHangoutList(id[0], id[0], mAuth);
			if(mHangout == null) {
				return false;
			}
			return true;
		}
		
		@Override
		protected void onPostExecute(Boolean result) {
			if(result) {
				mHangoutAdapter = new HangoutListAdapter(mContext,
						R.layout.hangout_entry, R.id.desc, mHangout, mInflater);
				vHangoutList.setAdapter(mHangoutAdapter);
				vHangoutList.setOnItemClickListener(new OnItemClickListener() {
                	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {         
                		Intent i = new Intent(HangoutBrowseActivity.this, HangoutActivity.class); 
                		i.putExtra("id", mHangout.get(position).id);
                		startActivity(i);
                	}
                });
				mProgressDialog.dismiss();
    		} else {
    			Toast.makeText(mContext, "Error: Connection with server failed!", Toast.LENGTH_SHORT).show();
    			mProgressDialog.dismiss();
    		}
	    }
		
	}

	
}
